//
//  BaseViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import Alamofire
import Toast_Swift
enum ShowNavigationRightButton {
    case share
    case collection
    case all
}

class BaseViewController: UIViewController, UIGestureRecognizerDelegate {
    var showRightButtonType: ShowNavigationRightButton = .all
    var delegate: UIGestureRecognizerDelegate?
    var collectionButton = UIButton(type: .custom)
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.delegate = self
        
        setNavigation()
        // Do any additional setup after loading the view.
    }
    
    public func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        
        if navigationController.viewControllers.count > 1 {
            let button = UIButton()
            button .setImage(UIImage(named: "all_return"), for: .normal)
            button.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0)
            button.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
            button.addTarget(self, action: #selector(returnVC), for: .touchUpInside)
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
        }
    }
    
    @objc func returnVC() {
        self.navigationController?.popViewController(animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if self.navigationController!.viewControllers.count > 1 {
            delegate = self.navigationController?.interactivePopGestureRecognizer?.delegate
            self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = delegate
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.navigationController!.childViewControllers.count > 1
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let controller = self.navigationController else { return false }
        
       return controller.viewControllers.count > 1
    }
    
    func setNavigation() {
        let shareItem = UIBarButtonItem(image: UIImage(named: "btn_share"), style: .done, target: self, action: #selector(shareTap))
        
        collectionButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        collectionButton.setImage(UIImage(named: "collection_unSelect"), for: .normal)
        collectionButton.setImage(UIImage(named: "collection_select"), for: .selected)
        collectionButton.addTarget(self, action: #selector(shoppingCartTap(_ :)), for: .touchUpInside)
        let collection = UIBarButtonItem(customView: collectionButton)
        switch showRightButtonType {
        case .all:
            self.navigationItem.rightBarButtonItems = [shareItem, collection]
        case .collection:
              self.navigationItem.rightBarButtonItem = collection
        case .share:
            self.navigationItem.rightBarButtonItem = shareItem
        }
    }
    
    @objc func shareTap() {
//        //创建分享消息对象
//        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//        //创建图片内容对象
//        UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//        //如果有缩略图，则设置缩略图
//        shareObject.thumbImage = [UIImage imageNamed:@"icon"];
//        [shareObject setShareImage:@"https://mobile.umeng.com/images/pic/home/social/img-1.png"];
//        //分享消息对象设置分享内容对象
//        messageObject.shareObject = shareObject;
        let message:UMSocialMessageObject = UMSocialMessageObject()
        let share: UMShareWebpageObject = UMShareWebpageObject.shareObject(withTitle: "标题", descr: "内容弟娃。。。。。。。。。", thumImage: UIImage(named: "add_image_icon"))
        share.webpageUrl = "http://www.baidu.com"
        message.shareObject = share

        ONShareManager.setPreDefinePlatforms([NSNumber(integerLiteral: UMSocialPlatformType.wechatSession.rawValue)
, NSNumber(integerLiteral: UMSocialPlatformType.QQ.rawValue), NSNumber(integerLiteral: UMSocialPlatformType.sina.rawValue), NSNumber(integerLiteral: UMSocialPlatformType.wechatTimeLine.rawValue)])
        ONShareManager.showShareMenuViewInWindowWithPlatformSelectionBlock { (platformType, info) in
            UMSocialManager.default().share(to: platformType, messageObject: message, currentViewController: self) { (shareResponse, error) in

                print("111");
//                completion(shareResponse, error);
            }
        }
//        ShareView().showShareView()
    }
    
    @objc func shoppingCartTap(_ btn: UIButton) {
      btn.isSelected = !btn.isSelected
    }
    
    func showToast(msg :String) -> Void {
        self.view.makeToast("一度蜜：" + msg, duration: 1.5, position: .center)
    }
    
    func removeHTML(htmlString : String)->String{
        return htmlString.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
    }
    
    func isValidPwd(pwd:NSString) -> Bool {
        let regex = "^\\S{8,20}$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: pwd)
        
    }
    func isValidPhone(phone:NSString) -> Bool {
        let regex = "^(1[0-9][0-9])\\d{8}$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: phone)
        
    }
    func isValidCard(card:NSString) -> Bool {
        let regex = "^(\\d{14}|\\d{17})(\\d|[xX])$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: card)
        
    }
    func isValidMail(mail:NSString) -> Bool {
        let regex = "^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: mail)
        
    }
    func isValidPass(text: String) -> Bool{
        let regex = "^[A-Za-z0-9]+$";
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: text)
    }
    func getHead() -> HTTPHeaders {
        let headers: HTTPHeaders = [
            "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
            "Accept": "application/json",
            "token":""
        ]
        return headers
    }
}
extension String {
    func deleteHTMLTag(tag:String) -> String {
        return self.replacingOccurrences(of: "(?i)</?\(tag)\\b[^<]*>", with: "", options: .regularExpression, range: nil)
    }
    
    func deleteHTMLTags(tags:[String]) -> String {
        var mutableString = self
        for tag in tags {
            mutableString = mutableString.deleteHTMLTag(tag: tag)
        }
        return mutableString
    }
    
    ///去掉字符串标签
    mutating func filterHTML() -> String?{
        let scanner = Scanner(string: self)
        var text: NSString?
        while !scanner.isAtEnd {
            scanner.scanUpTo("<", into: nil)
            scanner.scanUpTo(">", into: &text)
            self = self.replacingOccurrences(of: "\(text == nil ? "" : text!)>", with: "")
        }
        return self
    }
}
